package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 前端目的地控制器
 */
@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyContentService strategyContentService;
    @Reference
    private ITravelService travelService;
    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("hotRegions",regionService.selectHotRegion());
        return "/destination/index";
        }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Long regionId,Model model){
        //查询数据
        List<Destination> list = destinationService.getDestByRegionId(regionId);
         model.addAttribute("regionId",regionId);
        //拼接html块

        //页面再拼接Html块
        int size = list.size();
        //leftDests-children
        model.addAttribute("leftDests",list.subList(0,size/2+1));
        //rightDests
        model.addAttribute("rightDests",list.subList(size/2+1,size));

        return "/destination/hotdestTpl";
    }

    @RequestMapping("guide")
    public String guide(Model model,Long id){
        //吐司：中国>>广东>>广州
        //中国 广东需要显示热门子目的地，广州不需要
        //中国 广东 广州需要分开处理
        //toasts
        List<Destination> toasts = destinationService.getToasts(id);
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
         //根据目的地的id查询攻略目录
        model.addAttribute("catalogs",strategyCatalogService.selectCatalogByDestId(id));
        //根据目的地id去查询攻略明细
        List<StrategyDetail> strategyDetails = strategyDetailService.selectDetailTop3ByDestId(id);

        model.addAttribute("strategyDetails",strategyDetails);
        //dest
        model.addAttribute("dest",destination);
        return "/destination/guide";
    }

    @RequestMapping("surveyPage")
    public String surveryPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        return "/destination/survey";
    }

    @RequestMapping("survey")
    public String survery(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.selectCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",strategyCatalogs);

        StrategyCatalog catalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",catalog);

         if(catalog != null &&  catalog.getDetails().size() >0 && catalog.getDetails() != null){
             StrategyDetail strategyDetail = catalog.getDetails().get(0);
             strategyDetail.setStrategyContent(strategyContentService.selectContentByPrimaryKey(strategyDetail.getId()));
             model.addAttribute("detail",strategyDetail);
         }
         return "destination/surveyTpl";
    }

    @RequestMapping("travels")
    public String travel(Model model, @ModelAttribute("qo") TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/destination/travelTpl";
    }


}
